package com.lanswon.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.common.enums.ArticleStatus;
import com.lanswon.common.enums.ArticleType;
import com.lanswon.common.enums.ProblemType;
import com.lanswon.common.util.StringUtil;
import com.lanswon.entity.ArticleVO;
import com.lanswon.entity.CommonProblem;
import com.lanswon.entity.HostBank;
import com.lanswon.service.CommonProblemService;
import com.lanswon.service.HostbankService;

@Controller
@RequestMapping("admin/hostbank")
public class AdminHostBankController {
	
	@Autowired
	private HostbankService hostbankService;
	
	@RequestMapping("index/{hostbankType}")
	public String index(HttpServletRequest request,HttpServletResponse response,
			@PathVariable(value="hostbankType")Integer hostbankType,ModelMap modelMap) {
		Map<String, Object>  accessMap = new HashMap<>();
		accessMap.put("hostbankType", hostbankType);
		List<HostBank> hbList = hostbankService.findHostbankByCondition(accessMap);
		if(hbList!=null&&hbList.size()>0) {
			modelMap.addAttribute("hostbank", hbList.get(0));
		}
		modelMap.addAttribute("hostbankType", hostbankType);
		
		return "admin/hostbank/index";
	}
	
	
	@RequestMapping("updateHostbank")
	@ResponseBody
	public Map<String, Object> updateHostbank(HttpServletRequest request,HttpServletResponse response, HostBank hostbank){
		Map<String, Object> viewMap = new HashMap<>();
		try {
			System.out.println(hostbank.getReleaseTimeStr());
			if(hostbank.getReleaseTimeStr()!=null) {
				hostbank.setReleaseTime(StringUtil.strToDate(hostbank.getReleaseTimeStr(), "yyyy-MM-dd"));
			}
			if(hostbank.getId()!=null) {
				hostbankService.updateHostbank(hostbank);
			}else {
				hostbankService.addHostbank(hostbank);
			}
			viewMap.put("code", 1);
			viewMap.put("msg", "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			viewMap.put("code", 2);
			viewMap.put("msg", "保存失败");
		}
		return viewMap;
	}
	
}
